package com.china08.yxyapi.api.manage.tec.office.moments;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMoments;
import com.china08.yxyapi.entity.mg.moments.YxyMomentsComment;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsCommentRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsPraiseRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 班级圈--查看全部发布/我的发布
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController("MomentViewReleaseController")
public class MomentsViewReleaseController extends YxyApiBaseController {

    @Autowired
    private YxyMomentsRepository yxyMomentsRepository;

    @Autowired
    private YxyMomentsCommentRepository yxyMomentsCommentRepository;

    @Autowired
    private YxyMomentsPraiseRepository yxyMomentsPraiseRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/student/srh/release/view", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = true) ReqModel reqModel, RespModel respModel) {

        YxyProfile yxyProfile = getProfile(request, response);
        // 用户ID
        String currentUserId = yxyProfile.getId();
        Integer type = reqModel.type;
        // List<String> classIdLst = Arrays.asList(new String[] {
        // reqModel.classId });
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyMoments> page = null;

        // 班级 为空 或者 "" 时 查全部 否则 查某一个班级的班级圈消息
        String classId = reqModel.classId;
        // type 类型 -0 全部 1 我的发布
        if (type == 0) {// 全部
            if (classId == null || "".equals(classId.trim())) {// 查询全部
                page = yxyMomentsRepository.findBySchoolIdAndStatus(yxyProfile.getSchoolId(), 1, pageable);
            } else {// 查询某个班级
                page = yxyMomentsRepository.findByClassIdOrderByCreatedDateDesc(classId, pageable);
            }
        } else {// 我的发布
            if (classId == null || "".equals(classId.trim())) {// 查询全部
                page = yxyMomentsRepository.findBySenderIdOrderByCreatedDateDesc(currentUserId, pageable);
            } else {// 查询某个班级
                page = yxyMomentsRepository.findByClassIdAndSenderIdOrderByCreatedDateDesc(classId, currentUserId,
                        pageable);
            }
        }

        // 2. 转存在model中
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        List<YxyMomentsCommentModel> yxyMomentsCommentLst = null;
        List<YxyMomentsComment> yxyMomentsComment = new ArrayList<YxyMomentsComment>();
        // YxyMomentsCommentModel yxyMomentsCommentModel = null;
        List<YxyMomentsCommentModel> yxyMomentsCommentModelLst = null;

        for (YxyMoments yxyMoments : page) {
            respModel = new RespModel();
            String momentsId = yxyMoments.getId();
            respModel.id = momentsId;
            respModel.createdDate = DateToolUtils.format(yxyMoments.getCreatedDate(), "yyyy-MM-dd HH:mm");
            respModel.senderId = yxyMoments.getSenderId();
            respModel.senderNick = yxyMoments.getSenderNick();
            respModel.classNick = yxyMoments.getClassNick();
            respModel.senderUsername = yxyMoments.getSenderUsername();
            respModel.styleIntro = yxyMoments.getStyleIntro();

            // 图片处理
            List<String> imgList = yxyMoments.getImgs();
            for (String string : imgList) {
                string = ThumborUtil.resize(string, 60, 60);
            }
            respModel.imgs_60_60 = imgList;
            respModel.status = yxyMoments.getStatus();
            respModel.commentCount = yxyMoments.getCommentCount() != null ? yxyMoments.getCommentCount() : 0;//
            respModel.praiseCount = yxyMoments.getPraiseCount() != null ? yxyMoments.getPraiseCount() : 0;//
            respModel.praiseUserNickStr = yxyMoments.getPraiseUserNickStr("、 ");
            respModel.avator = yxyMoments.getAvatar();
            respModel.ownMoments = currentUserId.equals(yxyMoments.getSenderId());
            if (yxyMoments.getImgs() != null) {
                respModel.imgSize = yxyMoments.getImgs().size();
            }
            // 是否点赞
            respModel.praise = yxyMomentsPraiseRepository.countByMomentsIdAndUserId(momentsId, currentUserId) > 0;
            //
            yxyMomentsCommentLst = new ArrayList<YxyMomentsCommentModel>();
            yxyMomentsComment = yxyMomentsCommentRepository
                    .findFirst10ByMomentsIdOrderByCreatedDate(yxyMoments.getId());
            yxyMomentsCommentModelLst = new ArrayList<YxyMomentsCommentModel>();
            for (YxyMomentsComment yxyMomentsComment2 : yxyMomentsComment) {
                RespModel model = new RespModel();
                YxyMomentsCommentModel yxyMomentsCommentModel = new YxyMomentsCommentModel();
                yxyMomentsCommentModel.id = yxyMomentsComment2.getId();
                yxyMomentsCommentModel.userId = yxyMomentsComment2.getUserId();
                yxyMomentsCommentModel.userNick = yxyMomentsComment2.getUserNick();
                yxyMomentsCommentModel.avatar = yxyMomentsComment2.getAvatar();
                yxyMomentsCommentModel.title = yxyMomentsComment2.getReplyerNick();
                yxyMomentsCommentModel.comment = yxyMomentsComment2.getComment();
                yxyMomentsCommentModel.BefPre = yxyMomentsComment2.getBefPre();
                //
                // 是否自己的班级圈 （前端以此判断是否显示删除）
                yxyMomentsCommentModel.ownMoments = yxyMomentsCommentModel.userId.equals(currentUserId);
                yxyMomentsCommentModelLst.add(yxyMomentsCommentModel);

            }
            respModel.yxyMomentsCommentLst = yxyMomentsCommentModelLst;
            respModelLst.add(respModel);
        }
        Page<RespModel> model = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());

        return model;
    }

    @ApiModel(value = "MomentViewReq")
    protected static class ReqModel extends YxyApiPageReqModel {
        // 0 全部 1我的发布
        @ApiModelProperty(value = "类型")
        public Integer type;

        @ApiModelProperty(value = "班级ids")
        public String classId; // 如果为空查询全部班级

        @Override
        public void autoCheck() {
        }
    }

    /**
     * 评论列表
     * 
     * @author LiuWeidong
     *
     */
    public class YxyMomentsCommentModel {

        public Boolean ownMoments;
        public String id;
        // 评论用户ID
        public String userId;
        // 姓名
        public String userNick;
        // 头像
        public String avatar;
        // 标题
        public String title;
        // 评论信息
        public String comment;
        // 距离现在多久
        public String BefPre;
    }

    protected static class RespModel extends YxyApiRespModel {
        // ID
        public String id;
        // 创建时间
        public String createdDate;
        // 发送人头像
        public String avator;
        // 发送ID
        public String senderId;
        // 发送昵称
        public String senderNick;
        // 班级昵称
        public String classNick;
        // 发送名称
        public String senderUsername;
        // 发送内容
        public String styleIntro;
        // 发送图片
        public List<String> imgs_60_60;
        // 图片数量
        public Integer imgSize = 0;
        // 删除状态：0/1
        public Integer status;
        // 评论数量
        public Integer commentCount;//
        // 点赞数量
        public Integer praiseCount;//
        // 点赞人信息
        public String praiseUserNickStr;
        // 是否点赞
        public boolean praise;
        // 评论人列表
        public List<YxyMomentsCommentModel> yxyMomentsCommentLst;
        // 是否自己的班级圈
        public boolean ownMoments;
    }

}
